<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Checkbox Demo</title>
    <style>
      div {
        text-align: center;
      }
      .content {
        margin: 200px auto;
      }
      input {
        width: 20px;
        height: 20px;
      }
      #select {
        position: absolute;
        top: 0;
        left: 0;
        visibility: hidden;
        border: 2px dotted rgb(56, 57, 57);
      }
    </style>
  </head>

  <body>
    <div class="content">
      <div>
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
      </div>
      <div>
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
      </div>
      <div>
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
      </div>
      <div>
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
      </div>
      <div>
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
      </div>
      <div>
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="checkbox" />
      </div>
    </div>
    <div id="select"></div>

    <script>
      let flag = false;
      let items = document.getElementsByTagName("input");
      let select = document.getElementById("select");
      let startX = 0;
      let startY = 0;

      window.onmousedown = function (e) {
        flag = true;
        startX = e.x;
        startY = e.y;
      };

      window.onmousemove = function (e) {
        e.preventDefault();
        let minX = 0;
        let minY = 0;
        let maxX = 0;
        let maxY = 0;
        if (flag) {
          minX = Math.min(e.x, startX);
          minY = Math.min(e.y, startY);
          maxX = Math.max(e.x, startX);
          maxY = Math.max(e.y, startY);

          //通过选择框的边界及复选框的位置判断选择框内是否包含复选框
          for (let i = 0; i < items.length; i++) {
            let offLeft = items[i].offsetLeft;
            let offTop = items[i].offsetTop;
            let offWidth = items[i].offsetWidth;
            let offHeight = items[i].offsetHeight;
            if (
              offLeft >= minX &&
              offLeft + offWidth <= maxX &&
              offTop >= minY &&
              offTop + offHeight <= maxY
            ) {
              items[i].checked = true;
            } else {
              items[i].checked = false;
            }
          }
          //设置复选框的位置及大小
          select.style.width = maxX - minX + "px";
          select.style.height = maxY - minY + "px";
          select.style.left = minX + "px";
          select.style.top = minY + "px";
          select.style.visibility = "visible";
        }
      };

      window.onmouseup = function (e) {
        flag = false;
        select.style.visibility = "hidden";
      };
    </script>
  </body>
</html>
